using System;
using System.Xml;
using System.IO;
using System.Reflection;

using Icodeon;

using NUnit.Framework;

namespace Icodeon.Util
{
    [TestFixture]
    public class IOUtilsTest
    {
        [TestFixtureSetUp]
        public void Init()
        {
        }

        [TestFixtureTearDown]
        public void Dispose()
        {
        }

        [Test]
        public void TestReplaceParameter()
        {
            try
            {
                IOUtils ioUtils = new IOUtils();
                string paramterizedString = "scorm2cc/scorm${version}cc.xslt";
                string version = "1.2";
                string result = ioUtils.ReplaceParameter(paramterizedString, version);
                Assert.AreEqual("scorm2cc/scorm1.2cc.xslt", result);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }
        }

        [Test]
        public void TestGetApplicationDirectory()
        {
            try
            {
                IOUtils ioUtils = new IOUtils();
                DirectoryInfo di = ioUtils.GetApplicationDirectory();
                System.Console.WriteLine(di.FullName);

            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }
        }

        [Test]
        public void TestGetFileNameWithoutExtension()
        {
            try
            {
                IOUtils ioUtils = new IOUtils();
                string result = ioUtils.GetFileNameWithoutExtension(new FileInfo("file.html"));
                Assert.AreEqual("file", result);

            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }
        }

        [Test]
        public void TestCombineDirectoryNameAndFileName()
        {
            try
            {
                string directoryPath = @"C:\Users\icodeon\Documents\icodeon\projects\scorm2cc\codeplex\packages";
                DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);

                IOUtils ioUtils = new IOUtils();

                string relativePath = "file.html";
                FileInfo fileInfo = ioUtils.CombineDirectoryNameAndFileName(directoryInfo, relativePath);
                Assert.AreEqual(@"C:\Users\icodeon\Documents\icodeon\projects\scorm2cc\codeplex\packages\file.html", 
                    fileInfo.FullName);

                relativePath = "./file.html";
                fileInfo = ioUtils.CombineDirectoryNameAndFileName(directoryInfo, relativePath);
                Assert.AreEqual(@"C:\Users\icodeon\Documents\icodeon\projects\scorm2cc\codeplex\packages\file.html",
                    fileInfo.FullName);

                relativePath = "../file.html";
                fileInfo = ioUtils.CombineDirectoryNameAndFileName(directoryInfo, relativePath);
                Assert.AreEqual(@"C:\Users\icodeon\Documents\icodeon\projects\scorm2cc\codeplex\file.html",
                    fileInfo.FullName);

                relativePath = "../../../file.html";
                fileInfo = ioUtils.CombineDirectoryNameAndFileName(directoryInfo, relativePath);
                Assert.AreEqual(@"C:\Users\icodeon\Documents\icodeon\projects\file.html",
                    fileInfo.FullName);


            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }
        }

        [Test]
        public void TestRemoveUrlParameterString()
        {
            try
            {
                string relativePath = "../directory/file.html";
                string relativePathWithParams = "../directory/file.html?a=1&b=2";

                IOUtils ioUtils = new IOUtils();
                string result = ioUtils.RemoveUrlParameterString(relativePath);
                Assert.AreEqual(relativePath, result);

                result = ioUtils.RemoveUrlParameterString(relativePathWithParams);
                Assert.AreEqual(relativePath, result);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }
        }
    }
}
